
package com.feib.soeasy.job.importor;

import java.util.List;

import com.feib.soeasy.job.service.ImportService;
import com.feib.soeasy.model.ColumnDefinition;
import com.feib.soeasy.model.ColumnValidateError;
import com.feib.soeasy.model.FormatDefinition;
import com.feib.soeasy.model.PayerTemp;
import com.feib.soeasy.model.Payment;
import com.feib.soeasy.model.UploadFile;
import com.feib.soeasy.service.PayerTempService;


/**
 * @title (#)BaseValidatable.java<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/11/27 create by Jimmy Liu<br>
 */
public abstract class BaseValidatable 
{    
    private List<ColumnValidateError> columnValidateErrors;
    
    /**
     * 檔案
     */
    protected UploadFile uploadFile;
    
    /**
     * 格式定義
     */
    protected FormatDefinition formatDefinition;
    
    protected ImportService importService;
    
    /*protected PayerTempService payerTempService;*/
        
    /**
     * 檢核
     * @param payment 繳費單物件
     * @param lineNumber 第幾行
     * @return
     */
    public abstract boolean validate(Payment payment, long lineNumber);
    
    public BaseValidatable(UploadFile uploadFile, FormatDefinition formatDefinition, List<ColumnValidateError> columnValidateErrors, ImportService importService)
    {
        this.uploadFile = uploadFile;
        this.formatDefinition = formatDefinition;
        this.columnValidateErrors = columnValidateErrors;
        this.importService = importService;
    }
    
    /**
     * @param uploadFile2
     * @param formatDefinition2
     * @param columnValidateErrors2
     * @param importService2
     * @param payerTempService
     */
   /* public BaseValidatable(UploadFile uploadFile2, FormatDefinition formatDefinition2, List<ColumnValidateError> columnValidateErrors2, ImportService importService2, PayerTempService payerTempService) {
        this.uploadFile = uploadFile2;
        this.formatDefinition = formatDefinition2;
        this.columnValidateErrors = columnValidateErrors2;
        this.importService = importService2;
        this.payerTempService = payerTempService;
    }*/

    /**
     * 加入一筆檢核錯誤原因，若累積超過50筆資料，則整批新增到資料庫中
     * @param columnName 錯誤欄位
     * @param errorMessage 錯誤原因
     * @param exceptionMessage 程式錯誤原因
     */
    protected void addColumnValidateError(long lineNumber, String columnName, String errorMessage, Exception exceptionMessage)
    {        
        ColumnValidateError error = new ColumnValidateError();
        error.setUploadFile(uploadFile);
        error.setLineNumber(lineNumber);
        error.setColumnName(columnName);
        error.setErrorMessage(errorMessage);
        if (null != exceptionMessage)
            error.setExceptionMessage(exceptionMessage.getMessage());        
        columnValidateErrors.add(error);
    }
    
    /**
     * 以欄位名稱取得欄位標題
     * @param columnName 欄位名稱
     * @return
     */
    protected String getColumnTitle(String columnName)
    {
        List<ColumnDefinition> cds = formatDefinition.getColumnDefinitions();
        for (ColumnDefinition cd : cds)
        {
            if (cd.getColumnName().equalsIgnoreCase(columnName))
            {
                int beginIndex = (null == cd.getBeginIndex() ? 0 : cd.getBeginIndex().intValue());
                int endIndex = (null == cd.getEndIndex() ? 0 : cd.getEndIndex().intValue());
                return cd.getColumnTitle() + "("+beginIndex + "-" + endIndex +")";
            }
        }
        return null;
    }

    /**
     * @param payerTemp
     * @param lineNumber
     * @return
     */
    public boolean payerTempValidate(PayerTemp payerTemp, long lineNumber) {
        // TODO Auto-generated method stub
        return false;
    }

 

        
    
}
